/* eslint-disable @typescript-eslint/no-object-literal-type-assertion */
import { mallApi } from '/api/mall'
import { GlobalData } from '/store/state'
import { knowledgeApi } from '/api/konwledge'
import { $event } from '/utils'
import { informationApi } from '/api/information'
import { knowAboutApi } from '/api/knowAbout'

Page({
  data: {
    /** 商品的queries */
    cQuery: {
      limit: 20,
      offset: 0,
      liked: 'only',
      loading: false,
      canLoad: true,
      with: 'brands',
    },
    /** 知识的queries */
    kQuery: {
      limit: 20,
      offset: 0,
      liked: 'only',
      loading: false,
      canLoad: true,
    } as KnowledgeOptions,
    /** 资讯的queries */
    iQuery: {
      limit: 20,
      offset: 0,
      list_type: 'only_favorited',
      with: 'author',
      loading: false,
      canLoad: true,
    },
    /** 问答的queries */
    wQuery: {
      limit: 20,
      offset: 0,
      list_type: 'only_favorited',
      favorited: 'check',
      firstAnswer: true,
      loading: false,
      canLoad: true,
    },
    c: [] as any, // 商品
    k: [] as any, // 知识
    i: [] as any, // 资讯
    w: [] as DomoKnowAbout[], //
    currentTab: 'c',
    cLoaded: false,
    kLoaded: false,
    iLoaded: false,
    wLoaded: false,
    navigationHeight: GlobalData.navigationHeight,
    currencyName: GlobalData.currencyName,
  },
  async onLoad() {
    $event.on('after:boot', this, () => {
      this.setData({
        currencyName: GlobalData.currencyName,
        navigationHeight: GlobalData.navigationHeight,
      })
    })
    this.setData!({
      currencyName: GlobalData.currencyName,
      navigationHeight: GlobalData.navigationHeight,
    })
  },
  onShow() {
    this.fetchCommodities(null)
  },
  onReachBottom() {
    this.loadMore('loadMore')
  },
  onPullDownRefresh() {
    this.loadMore('refresh')
  },
  /** 获取收藏的知识 */
  fetchKnowledge(loadType: 'loadMore' | 'refresh' | null) {
    const {
      data: { kQuery },
    } = this
    let {
      data: { k },
    } = this
    // 上拉加载更多的判断
    if (!kQuery.canLoad && loadType !== 'refresh') {
      return
    }
    // 是否还在执行上一次加载
    if (!kQuery.loading) {
      this.setData({
        kQuery: {
          ...this.data.kQuery,
          loading: true,
        },
      })
      loadType === 'refresh' && (kQuery.offset = 0)
      knowledgeApi.getKnowledge(kQuery).then((data) => {
        kQuery.loading = false
        k =
          loadType === 'loadMore'
            ? [...k, ...data]
            : loadType === 'refresh'
              ? this.resolveRefresh('k', data)
              : data
        kQuery.offset = loadType === 'refresh' ? k.length : kQuery.offset + data.length
        if (loadType !== 'refresh') {
          kQuery.canLoad = data.length === kQuery.limit
        }
        this.setData({
          kQuery,
          k,
          kLoaded: true,
        })
        loadType === 'refresh' && wx.stopPullDownRefresh()
      })
    }
  },
  /** 获取收藏的资讯 */
  fetchInfo(loadType: 'loadMore' | 'refresh' | null) {
    const {
      data: { iQuery },
    } = this
    let {
      data: { i },
    } = this
    // 上拉加载更多的判断
    if (!iQuery.canLoad && loadType !== 'refresh') {
      return
    }
    // 是否还在执行上一次加载
    if (!iQuery.loading) {
      this.setData({
        iQuery: {
          ...this.data.iQuery,
          loading: true,
        },
      })
      loadType === 'refresh' && (iQuery.offset = 0)
      informationApi.getInformationCategoriesList(iQuery).then((data) => {
        iQuery.loading = false
        i =
          loadType === 'loadMore'
            ? [...i, ...data]
            : loadType === 'refresh'
              ? this.resolveRefresh('i', data)
              : data
        iQuery.offset = loadType === 'refresh' ? i.length : iQuery.offset + data.length
        if (loadType !== 'refresh') {
          iQuery.canLoad = data.length === iQuery.limit
        }
        this.setData({
          iQuery,
          i,
          iLoaded: true,
        })
        loadType === 'refresh' && wx.stopPullDownRefresh()
      })
    }
  },
  /** 获取收藏的问题 */
  fetchknowAbout(loadType: 'loadMore' | 'refresh' | null) {
    const {
      data: { wQuery },
    } = this
    let {
      data: { w },
    } = this
    // 上拉加载更多的判断
    if (!wQuery.canLoad && loadType !== 'refresh') {
      return
    }
    // 是否还在执行上一次加载
    if (!wQuery.loading) {
      this.setData({
        wQuery: {
          ...this.data.wQuery,
          loading: true,
        },
      })
      loadType === 'refresh' && (wQuery.offset = 0)
      knowAboutApi.getKnowAboutList(wQuery).then((data) => {
        wQuery.loading = false
        w =
          loadType === 'loadMore'
            ? [...w, ...data]
            : loadType === 'refresh'
              ? this.resolveRefresh('w', data)
              : data
        wQuery.offset = loadType === 'refresh' ? w.length : wQuery.offset + data.length
        if (loadType !== 'refresh') {
          wQuery.canLoad = data.length === wQuery.limit
        }
        this.setData({
          wQuery,
          w,
          wLoaded: true,
        })
        loadType === 'refresh' && wx.stopPullDownRefresh()
      })
    }
  },
  /**
   * loadType
   */
  fetchCommodities(loadType: 'refresh' | 'loadMore' | null) {
    const {
      data: { cQuery },
    } = this
    let {
      data: { c },
    } = this
    // 如果不能加载更多
    if (!cQuery.canLoad && loadType !== 'refresh') {
      return
    }
    // 未加载状态
    if (!cQuery.loading) {
      // 设置加载状态
      this.setData({
        cQuery: {
          ...this.data.cQuery,
          loading: true,
        },
      })
      loadType === 'refresh' && (cQuery.offset = 0)
      mallApi.getCommodities(cQuery).then((data) => {
        cQuery.loading = false

        c =
          loadType === 'loadMore'
            ? [...c, ...data]
            : loadType === 'refresh'
              ? this.resolveRefresh('c', data)
              : data

        cQuery.offset = loadType === 'refresh' ? c.length : cQuery.offset + data.length
        if (loadType !== 'refresh') {
          cQuery.canLoad = data.length === cQuery.limit
        }

        this.setData({
          cQuery,
          c,
          cLoaded: true,
        })
        loadType === 'refresh' && wx.stopPullDownRefresh()
      })
    }
  },
  /**
   *
   * @param type 处理刷新页面的情况
   * @param data
   */
  resolveRefresh(type: 'c' | 'k' | 'i' | 'w', data: any) {
    return data
  },
  // tab切换
  clickTab(e: any) {
    if (this.data.currentTab === e.target.dataset.current) {
      return false
    } else {
      this.setData(
        {
          currentTab: e.target.dataset.current,
        },
        () => {
          if (e.target.dataset.current === 'k') {
            const {
              data: { kLoaded },
            } = this
            if (!kLoaded) {
              this.fetchKnowledge(null)
            }
          } else if (e.target.dataset.current === 'i') {
            const {
              data: { iLoaded },
            } = this
            if (!iLoaded) {
              this.fetchInfo(null)
            }
          } else if (e.target.dataset.current === 'w') {
            const {
              data: { wLoaded },
            } = this
            if (!wLoaded) {
              this.fetchknowAbout(null)
            }
          }
        },
      )
    }
    return false
  },
  /** 到达底部 加载更多 */
  loadMore(type: 'loadMore' | 'refresh'): void {
    const {
      data: { currentTab },
    } = this
    switch (currentTab) {
      case 'c':
        this.fetchCommodities(type)
        break
      case 'k':
        this.fetchKnowledge(type)
        break
      case 'i':
        this.fetchInfo(type)
        break
      case 'w':
        this.fetchknowAbout(type)
        break
    }
  },
})
